import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:wis_flutter/common/DataResult.dart';
import 'package:wis_flutter/common/http/HttpManage.dart';
import 'package:wis_flutter/wis_component/BI/BarChartSample1.dart'; // 条形图 1
import 'package:wis_flutter/wis_component/BI/BarChartSample2.dart'; // 条形图 2
import 'package:wis_flutter/wis_component/BI/LineChartSample1.dart'; // 条形图 1
import 'package:wis_flutter/wis_component/BI/PieChartSample1.dart'; // 饼图 1
import 'package:wis_flutter/wis_component/BI/PieChartSample2.dart'; // 饼图 2
import 'package:wis_flutter/wis_component/carousel/carousel_Page.dart'; // 轮播图
import 'package:wis_flutter/wis_component/checkBox.dart'; //  复选框
import 'package:wis_flutter/wis_component/datePickerForm.dart'; //  日期
import 'package:wis_flutter/wis_component/divider.dart'; // 线
import 'package:wis_flutter/wis_component/homeBody.dart'; // home页 body
import 'package:wis_flutter/wis_component/listTableCross.dart'; // table 横向
import 'package:wis_flutter/wis_component/listTableVertical.dart'; // table 纵向
import 'package:wis_flutter/wis_component/searchPage.dart'; // 查询页面
import 'package:wis_flutter/wis_component/selectForm.dart'; //  下拉框
import 'package:wis_flutter/wis_component/selectCorrelateForm.dart'; //  下拉框 联动

import 'package:wis_flutter/wis_component/slideBoxRight.dart'; // 左右滑动
import 'package:wis_flutter/wis_component/sliverAppBar.dart'; // 自定义 头部导航条
import 'package:wis_flutter/wis_component/text.dart'; // 标题栏
import 'package:wis_flutter/wis_component/textForm.dart'; //  input 输入框
import 'package:wis_flutter/wis_component/timePickerForm.dart'; //  时间
import 'package:wis_flutter/wis_component/dateAndTimePickerForm.dart'; // 日期 时间

import 'package:wis_flutter/wis_component/treeView.dart'; //  组织树页面
import 'package:wis_flutter/wis_component/card.dart'; //  卡片
import 'package:wis_flutter/wis_component/stepperCompont.dart'; //  步骤条
import 'package:wis_flutter/wis_component/stepperCompontVertical.dart'; //  步骤条 纵向
import 'package:wis_flutter/wis_component/customButtonBar.dart'; //  按钮组
import 'package:wis_flutter/wis_component/uploadPictures.dart'; //  图片上传
import 'package:wis_flutter/wis_component/uploadFile.dart'; //  文件 上传




import 'package:wis_flutter/wis_component/login_component.dart'; // 登录页




// 文件 上传
class WisUploadFileForm extends StatelessWidget {
  final dynamic controller; // 表单名称
  final String label; // lable
  final bool validator; // 校验
  final bool noAsync; // 同步

  final String validatorText; // 校验提示
  final bool readOnly; // 只读
  final double maxLimit; // 数量限制

  final dynamic onChanged;    // 值改变
  final dynamic onUpload;    // 上传 单个

  final dynamic onDeleteChildren;    // children 图片回调
  final dynamic onDelete;    // 删除 图片回调

  

  final List children;   // 自定义图片


  WisUploadFileForm({
    Key key,
    this.label,
    this.validator = false,
    this.noAsync = false,  
    this.validatorText = "必填",
    this.controller,
    this.readOnly = false,
    this.onChanged,
    this.onUpload,
    this.onDeleteChildren,
    this.onDelete,
    this.children,
    this.maxLimit=0

    
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return UploadFilePage(
      controller: this.controller,
      label: this.label,
      validator: this.validator,
      noAsync: this.noAsync, 
      validatorText: this.validatorText,
      readOnly: this.readOnly,
      onChanged: this.onChanged,
      onUpload: this.onUpload,
      onDeleteChildren: this.onDeleteChildren,
      onDelete: this.onDelete,
      children: this.children,
      maxLimit: this.maxLimit,


    );
  }
}





// 图片上传
class WisUploadPicturesForm extends StatelessWidget {
  final dynamic controller; // 表单名称
  final String label; // lable
  final bool validator; // 校验
  final String validatorText; // 校验提示
  final bool readOnly; // 只读
  final double maxLimit; // 数量限制

  final dynamic onChanged;    // 值改变
  final dynamic onDeleteChildren;    // children 图片回调

  final List children;   // 自定义图片
  final bool hideAddButton; // 隐藏添加按钮
  final bool hideCameraButton; // 隐藏相机按钮



  WisUploadPicturesForm({
    Key key,
    this.label,
    this.validator = false,
    this.validatorText = "必填",
    this.controller,
    this.readOnly = false,
    this.onChanged,
    this.onDeleteChildren,
    this.children,
    this.maxLimit=0,
    this.hideAddButton=false,
    this.hideCameraButton=false,


    
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return UploadPicturesPage(
      controller: this.controller,
      label: this.label,
      validator: this.validator,
      validatorText: this.validatorText,
      readOnly: this.readOnly,
      onChanged: this.onChanged,
      onDeleteChildren: this.onDeleteChildren,
      children: this.children,
      maxLimit: this.maxLimit,
      hideAddButton: this.hideAddButton,
      hideCameraButton: this.hideCameraButton,


    );
  }
}





// ******  按钮组
class WisButtonBar extends StatelessWidget {
  final List children;   // 按钮

  WisButtonBar({
    Key key,
    this.children,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomButtonBar(
      children: this.children,
    );
  }
}



// ******  步骤条 纵向
class WisStepperVertical extends StatelessWidget {

  final int currentStep;      // 当前第几步
  final double width;           // 宽度 
  final List children;   // 列表组
  final dynamic onClick;        // 点击事件



  WisStepperVertical({
    Key key,
    this.currentStep=0,
    this.children,
    this.onClick,
    this.width=410,

  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StepperCompontVertical(
      currentStep: this.currentStep,
      children: this.children,
      onClick: this.onClick,
      width: this.width,

    );
  }
}


// ******  步骤条 横向
class WisStepper extends StatelessWidget {

  final int currentStep;      // 当前第几步
  final List children;   // 列表组
  final dynamic onClick;        // 点击事件



  WisStepper({
    Key key,
    this.currentStep=0,
    this.children,
    this.onClick
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StepperCompont(
      currentStep: this.currentStep,
      children: this.children,
      onClick: this.onClick
    );
  }
}






// 全局 消息提醒
class WisMessageGlobal{

    // 初始化
    static _init({
      BuildContext context,
      String title,
      String content,
      IconData icon, 
      Color iconColor,
      int duration,
      dynamic buttonList,
      bool lock,
      dynamic onClose


    }){


      List<Widget> _list = [];
      bool _closeAction=true;
      // Context context = navigatorKey.currentState.overlay.context;

      // 自定义 按钮  
      if( buttonList != null ){

        // Widget content; 
        for(var item in buttonList ) {
          _list.add(
              FlatButton(
                child: Text(item["label"].toString()),
                color: item["color"]!=null?item["color"]:null,
                onPressed:(){
                  if( item["onClick"]!=null ){
                    item["onClick"](item);
                  }
                  // Navigator.of(context).pop(); 
                }
              ) 
          );
        }

      }


      showDialog<Null>(
          context: context,
          barrierDismissible: true,
          builder: (BuildContext context) {
              return AlertDialog(
                // title: Text('标题'),
                content: SingleChildScrollView(
                    child: ListBody(
                      children: <Widget>[              
                        Container(
                          margin: const EdgeInsets.fromLTRB(0,2,0,6),
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                flex: 3,
                                child: Icon(
                                  // Icons.check,
                                  icon,
                                  color: iconColor,
                                  size: 42.0,
                                )
                              ),
                              Expanded(
                                flex: 8,
                                child: Text(
                                  title!=null?title.toString():'成功！',
                                  textAlign: TextAlign.left,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: const Color.fromRGBO(0,0,0,1),
                                    fontWeight: FontWeight.w700,
                                    fontSize: 16
                                  )     
                                )
                              )
                            ],
                          )
                        ),
                        Container(
                          padding:content!=null? const EdgeInsets.fromLTRB(8,8,8,8):const EdgeInsets.fromLTRB(0,0,0,0),
                          child: content!=null? Text(
                            content.toString()
                          ):null,
                        )
                      ]
                    ),
                ),
                actions: _list.toList()
              );
          },
      ).then((val) {
        _closeAction=false;
        
        // 关闭回调
        if( onClose!=null ){
          onClose();
        }
      });  


      // 自动关闭
      if( _list.length==0 && (lock==false || lock==null) ){
        Future.delayed(new Duration(milliseconds: (duration!=null?duration:3000) ),(){
          if(_closeAction){
            Navigator.of(context).pop();
          }
        });
      }



    }

    // 成功
    static succeed({
      BuildContext context,
      String title,
      String content,
      IconData icon, 
      int duration,
      dynamic buttonList,
      bool lock,
      dynamic onClose
    }) {
      _init(
        icon: Icons.check_circle,
        iconColor: Color.fromRGBO(25,190,107,1),
        context:context,
        title:title,
        content:content,
        duration:duration,
        buttonList:buttonList,
        lock:lock,
        onClose:onClose
      );

    }

    // 警告
    static warning({
      BuildContext context,
      String title,
      String content,
      IconData icon, 
      int duration,
      dynamic buttonList,
      bool lock,
      dynamic onClose
    }) {
      _init(
        icon: Icons.warning,
        iconColor: Color.fromRGBO(255,153,0,1),
        context:context,
        title:title,
        content:content,
        duration:duration,
        buttonList:buttonList,
        lock:lock,
        onClose:onClose
      );

    }    

    // 失败
    static error({
      BuildContext context,
      String title,
      String content,
      IconData icon, 
      int duration,
      dynamic buttonList,
      bool lock,
      dynamic onClose
    }) {
      _init(
        icon: Icons.error_outline,
        iconColor: Color.fromRGBO(237,64,20,1),
        context:context,
        title:title,
        content:content,
        duration:duration,
        buttonList:buttonList,
        lock:lock,
        onClose:onClose
      );

    } 
    


}






// ********** 详情 卡片
class WisCardview1 extends StatelessWidget {
  final String title; // 标题
  final String content; // 内容
  final dynamic children; // 内容
  final dynamic defaultBtnConfig; // 默认按钮
  final dynamic buttonList; // 自定义 按钮



  WisCardview1({
    Key key,
    this.title,
    this.content,
    this.defaultBtnConfig,
    this.children,
    this.buttonList,

  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Cardview1(
      title:this.title,
      content:this.content,
      children:this.children,
      defaultBtnConfig:this.defaultBtnConfig,
      buttonList:this.buttonList,

    );    
  }
}




// 消息提醒
class WisMessage{

    // 成功
    static succeed({String text,IconData icon, int duration, bool floating}) {

      final snackBar = SnackBar(
        behavior: (floating!=null && floating==true)?SnackBarBehavior.floating:SnackBarBehavior.fixed, 
        backgroundColor: const Color.fromRGBO(25,190,107,1),
        content: Row(
          children: <Widget>[
            Icon(icon!=null?icon:Icons.check),
            Padding(
              padding: EdgeInsets.fromLTRB(6,0,0,0),
              child: Text(text!=null?text.toString():"操作成功"),
            )
          ],
        ),
        duration: Duration(milliseconds: (duration!=null?duration:700) ),  // 延时
        // action: SnackBarAction(
        //   label: '撤消',
        //   textColor: Colors.white,
        //   onPressed: () {}
        // ),
        // behavior: SnackBarBehavior.floating, 
      );

      return snackBar;
    }

    // 警告
    static warning({String text,IconData icon, int duration, bool floating}) {

      final snackBar = SnackBar(
        behavior: (floating!=null && floating==true)?SnackBarBehavior.floating:SnackBarBehavior.fixed, 
        backgroundColor: const Color.fromRGBO(255,153,0,1),
        content: Row(
          children: <Widget>[
            Icon(icon!=null?icon:Icons.warning),
            Padding(
              padding: EdgeInsets.fromLTRB(6,0,0,0),
              child: Text(text!=null?text.toString():"操作成功"),
            )
          ],
        ),
        duration: Duration(milliseconds: (duration!=null?duration:700) ),  // 延时
        // action: SnackBarAction(
        //   label: '撤消',
        //   textColor: Colors.white,
        //   onPressed: () {}
        // ),
        // behavior: SnackBarBehavior.floating, 
      );

      return snackBar;
    }

    // 失败
    static error({String text,IconData icon, int duration, bool floating}) {

      final snackBar = SnackBar(
        behavior: (floating!=null && floating==true)?SnackBarBehavior.floating:SnackBarBehavior.fixed, 
        backgroundColor: const Color.fromRGBO(237,64,20,1),
        content: Row(
          children: <Widget>[
            Icon(icon!=null?icon:Icons.close),
            Padding(
              padding: EdgeInsets.fromLTRB(6,0,0,0),
              child: Text(text!=null?text.toString():"操作成功"),
            )
          ],
        ),
        duration: Duration(milliseconds: (duration!=null?duration:700) ),  // 延时
        // action: SnackBarAction(
        //   label: '撤消',
        //   textColor: Colors.white,
        //   onPressed: () {}
        // ),
        // behavior: SnackBarBehavior.floating, 
      );

      return snackBar;
    }

    


}




// ********** 页面  组织树
class WisTreePage extends StatelessWidget {
  final double height;          // 高度
  final List children;     // 数据
  final dynamic onClick;        // 点击事件
  final dynamic onExpansionChanged;    // 展开事件



  WisTreePage({
    Key key,
      this.height=300,
      this.children,
      this.onClick,
      this.onExpansionChanged,

  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TreeviewPage(
        height: this.height,
        children: this.children,
        onClick: this.onClick,
        onExpansionChanged: this.onExpansionChanged,

    );    
  }
}



// 自定义 头部导航条
class WisSliverAppBar extends StatelessWidget {
  final String title; // 标题
  final List<Widget> actions; // 按钮
  final double height; // 高度
  final Widget backgroundImage; // 背景图片

  WisSliverAppBar(
      {Key key,
      this.title = 'WIS Home',
      this.actions,
      this.height = 200,
      this.backgroundImage})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SliverAppBarCustom(
        title: this.title,
        actions: this.actions,
        height: this.height,
        backgroundImage: this.backgroundImage);
  }
}

// 折线图 1
class WisLineChart1 extends StatelessWidget {
  WisLineChart1({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LineChartSample1();
  }
}

// 条形图 2
class WisBarChart2 extends StatelessWidget {
  WisBarChart2({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BarChartSample2();
  }
}

// 条形图 1
class WisBarChart1 extends StatelessWidget {
  WisBarChart1({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BarChartSample1();
  }
}

// 饼图 1
class WisPieChart1 extends StatelessWidget {
  WisPieChart1({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PieChartSample1();
  }
}

// 饼图 2
class WisPieChart2 extends StatelessWidget {
  WisPieChart2({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PieChartSample2();
  }
}

// ******  复选框
class WisCheckboxForm extends StatelessWidget {
  final dynamic controller; // 表单名称
  final String label; // lable
  final bool validator; // 校验
  final String validatorText; // 校验提示
  final bool readOnly; // 只读
  final bool multiple; // 多选
  final List<Map> children; // 数据
  final dynamic onSearch; // 查询回调
  final dynamic selectData; // 选中数据
  final bool initialData; // 表单返回选中的 原始数据
  final dynamic onChanged;    // 值改变


  WisCheckboxForm({
    Key key,
    this.label,
    this.validator = false,
    this.validatorText = "必填",
    this.controller,
    this.readOnly = false,
    this.multiple = false,
    this.children,
    this.onSearch,
    this.selectData,
    this.initialData = false,
    this.onChanged,

  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CheckboxForm(
        controller: this.controller,
        label: this.label,
        validator: this.validator,
        validatorText: this.validatorText,
        readOnly: this.readOnly,
        multiple: this.multiple,
        children: this.children,
        onSearch: this.onSearch,
        // selectData: this.selectData,
        initialData: this.initialData,
        onChanged: this.onChanged,

      );
  }
}

// 横线
class WisDivider extends StatelessWidget {
  final String text; // 文本

  WisDivider({
    Key key,
    this.text,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DividerCrosswise(
      text: this.text,
    );
  }
}

// 轮播图
class WisCarousel extends StatelessWidget {
  final List children; // 数据
  final double height; // 高度
  final bool autoScroll; // 是否轮播
  final int seconds; // 轮播间隔
  final dynamic onClick; // 点击事件
  final String type; // 类型

  WisCarousel(
      {Key key,
      this.children,
      this.height = 100,
      this.autoScroll = true,
      this.onClick,
      this.seconds = 5,
      this.type = 'image'})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CarouselPage(
        children: this.children,
        height: this.height,
        autoScroll: this.autoScroll,
        seconds: this.seconds,
        onClick: this.onClick,
        type: this.type);
  }
}

// 左右滑动盒子
class WisSlideBoxRight extends StatelessWidget {
  final List data; // 数据
  final dynamic onClick; // 点击事件

  WisSlideBoxRight({Key key, this.data, this.onClick}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SlideBoxRight(data: this.data, onClick: this.onClick);
  }
}


// 下拉框 联动
class WisSelectCorrelateForm extends StatelessWidget {
  final dynamic controller; // 表单名称
  final String label; // lable
  final String defaultVal; // 默认值
  final bool validator; // 校验
  final String validatorText; // 校验提示
  final bool readOnly; // 只读
  final dynamic onChanged;    // 值改变
  final dynamic children;    // 组织树 数据

  


  WisSelectCorrelateForm({
    Key key,
    this.label,
    this.defaultVal,
    this.validator = false,
    this.validatorText = "必填",
    this.controller,
    this.readOnly = false,
    this.onChanged,
    this.children,

    
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SelectCorrelateForm(
      controller: this.controller,
      label: this.label,
      validator: this.validator,
      validatorText: this.validatorText,
      readOnly: this.readOnly,
      onChanged: this.onChanged,
      children: this.children,
      defaultVal: this.defaultVal,

    );
  }
}


// 日期和时间
class WisDateAndTimePickerForm extends StatelessWidget {
  final dynamic controller; // 表单名称
  final String label; // lable
  final bool validator; // 校验
  final String validatorText; // 校验提示
  final bool readOnly; // 只读
  final dynamic onChanged;    // 值改变

  final DateTime firstDate; // 开始日期
  final DateTime lastDate; // 结束日期
  final dynamic formatDate; // 日期格式化



  WisDateAndTimePickerForm({
    Key key,
    this.label,
    this.validator = false,
    this.validatorText = "必填",
    this.controller,
    this.readOnly = false,
    this.onChanged,
    this.firstDate,
    this.lastDate,
    this.formatDate,    
    
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DateAndTimePickerFormForm(
      controller: this.controller,
      label: this.label,
      validator: this.validator,
      validatorText: this.validatorText,
      readOnly: this.readOnly,
      onChanged: this.onChanged,
      firstDate: this.firstDate,
      lastDate: this.lastDate,
      formatDate: this.formatDate,      

    );
  }
}


// 时间
class WisTimePickerForm extends StatelessWidget {
  final dynamic controller; // 表单名称
  final String label; // lable
  final bool validator; // 校验
  final String validatorText; // 校验提示
  final bool readOnly; // 只读
  final dynamic onChanged;    // 值改变


  WisTimePickerForm({
    Key key,
    this.label,
    this.validator = false,
    this.validatorText = "必填",
    this.controller,
    this.readOnly = false,
    this.onChanged,
    
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TimePickerForm(
      controller: this.controller,
      label: this.label,
      validator: this.validator,
      validatorText: this.validatorText,
      readOnly: this.readOnly,
      onChanged: this.onChanged,

    );
  }
}

// ******  datePicker 日期
class WisDatePickerForm extends StatelessWidget {
  final dynamic controller; // 表单名称
  final String label; // lable
  final bool validator; // 校验
  final String validatorText; // 校验提示
  final bool readOnly; // 只读
  final DateTime firstDate; // 开始日期
  final DateTime lastDate; // 结束日期
  final dynamic formatDate; // 日期格式化
  final dynamic onChanged;    // 值改变


  WisDatePickerForm(
      {Key key,
      this.label,
      this.validator = false,
      this.validatorText = "必填",
      this.controller,
      this.readOnly = false,
      this.firstDate,
      this.lastDate,
      this.formatDate,
      this.onChanged,
      
    })
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DatePickerForm(
        controller: this.controller,
        label: this.label,
        validator: this.validator,
        validatorText: this.validatorText,
        readOnly: this.readOnly,
        firstDate: this.firstDate,
        lastDate: this.lastDate,
        formatDate: this.formatDate,
        onChanged: this.onChanged,

      );
  }
}

// ******  下拉框
class WisSelectForm extends StatelessWidget {
  final dynamic controller; // 表单名称
  final String label; // lable
  final bool validator; // 校验
  final String validatorText; // 校验提示
  final bool readOnly; // 只读
  final bool multiple; // 多选
  final List children; // 数据
  final dynamic selectData; // 选中数据
  final dynamic onSearch; // 查询回调
  final bool initialData; // 表单返回选中的 原始数据
  final dynamic onChanged;    // 值改变
  final dynamic onSave;    // 保存回调



  WisSelectForm({
    Key key,
    this.label,
    this.validator = false,
    this.validatorText = "必填",
    this.controller,
    this.readOnly = false,
    this.multiple = false,
    this.children,
    this.selectData,
    this.onSearch,
    this.initialData = false,
    this.onChanged,
    this.onSave

  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SelectForm(
      controller: this.controller,
      label: this.label,
      validator: this.validator,
      validatorText: this.validatorText,
      readOnly: this.readOnly,
      multiple: this.multiple,
      children: this.children,
      // selectData: this.selectData,
      onSearch: this.onSearch,
      initialData: this.initialData,
      onChanged: this.onChanged,
      onSave: this.onSave
    );
  }
}

// ******  input 输入框
class WisTextForm extends StatelessWidget {
  final dynamic controller; // 表单名称
  final String label; // lable
  final bool validator; // 校验
  final String validatorText; // 校验提示
  final bool readOnly; // 只读
  final int maxLines; // 多行文本
  final dynamic onChanged;    // 值改变
  final dynamic inputFormatters;    // 正则校验
  final TextInputType keyboardType;   // 输入类型

  


  WisTextForm({
    Key key,
    this.label,
    this.validator = false,
    this.validatorText = "必填",
    this.controller,
    this.onChanged,
    this.readOnly = false,
    this.maxLines = 1,
    this.inputFormatters,
    this.keyboardType

  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextForm(
        controller: this.controller,
        label: this.label,
        validator: this.validator,
        onChanged: this.onChanged,
        validatorText: this.validatorText,
        readOnly: this.readOnly,
        maxLines: this.maxLines,
        inputFormatters: this.inputFormatters,
        keyboardType: this.keyboardType,

      );
  }
}

// ******  table 纵向
class WisListPageTableVertical extends StatelessWidget {
  final List data; // 数据

  WisListPageTableVertical({
    Key key,
    this.data,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListPageTableVertical(
      data: this.data,
    );
  }
}

// ******  table 横向
class WisListPageTableCross extends StatelessWidget {
  final List columns; // 列
  final List data; // 数据

  WisListPageTableCross({
    Key key,
    this.columns,
    this.data,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListPageTableCross(
      columns: this.columns,
      data: this.data,
    );
  }
}

// ******  标题栏
class WisTextLabelLine extends StatelessWidget {
  final String text; // text

  WisTextLabelLine({
    Key key,
    this.text,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextLabelLine(text: this.text);
  }
}

// ******  查询页面
class WisSearchPage extends StatelessWidget {
  final String label; // label
  final List data; // 数据
  final dynamic filterData; // 过滤数据
  final dynamic defaultClickConfig; // 默认按钮配置
  final dynamic headPopupMenu; // 头部按钮
  final dynamic onSearch; // 回调
  final dynamic onRowClick; // 行点击事件
  final dynamic onFilter; // 筛选点击
  final dynamic onHeadChange; // 头部按钮切换
  final dynamic onRefreshPullUp; // 上拉刷新
  final dynamic onRefresh;    // 下拉刷新
  final dynamic onselectAll;    // checkbox 选中回调
  final bool singleChoice;   // 单选
  final bool checkbox;   // 复选框
  final dynamic htmlPage; // 自定义页面
  final dynamic customButton; // 自定义按钮
  final dynamic onLoadingmore; // 加载更多
  final bool hideLoadingmore;   // 隐藏加载更多

  


  WisSearchPage({
    Key key,
    this.label = "请输入...",
    this.data,
    this.filterData,
    this.defaultClickConfig,
    this.headPopupMenu,
    this.onSearch,
    this.onRowClick,
    this.onFilter,
    this.onHeadChange,
    this.onRefreshPullUp,
    this.onRefresh,
    this.onselectAll,
    this.singleChoice=false,
    this.checkbox=false,
    this.hideLoadingmore=false,
    this.htmlPage,
    this.customButton,
    this.onLoadingmore


  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SearchPage(
      label: this.label,
      data: this.data,
      filterData: this.filterData,
      defaultClickConfig: this.defaultClickConfig,
      headPopupMenu: this.headPopupMenu,
      onRowClick: this.onRowClick,
      onFilter: this.onFilter,
      onSearch: this.onSearch,
      onHeadChange: this.onHeadChange,
      onRefreshPullUp: this.onRefreshPullUp,
      onRefresh: this.onRefresh,
      singleChoice: this.singleChoice,
      checkbox: this.checkbox,
      onselectAll: this.onselectAll,
      htmlPage: this.htmlPage,
      customButton: this.customButton,
      onLoadingmore: this.onLoadingmore,
      hideLoadingmore: this.hideLoadingmore

    );
  }
}

// ******  home页 body
class WisHomeBody extends StatelessWidget {
  final String title; // 标题
  final List children; // 列表组
  final dynamic onClick; // 点击事件
  final dynamic type; // 卡片类型

  WisHomeBody({Key key, this.type, this.children, this.onClick, this.title})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return HomePageBody(
        type: this.type,
        title: this.title,
        children: this.children,
        onClick: this.onClick);
  }
}

// ********** 页面 登录页
class WisLogonPage extends StatelessWidget {
  final String title; // 标题
  final Function onLogin; // 回调
  final dynamic widgetContent;   // 自定义 组件
  final dynamic defaultVal;       // 默认值 
  final dynamic backgroundImage;   // 背景图片
    final dynamic nameController;      // 用户名 表单
    final dynamic passwordController;      // 密码 表单


  WisLogonPage({
    Key key,
    this.title,
    this.onLogin,
    this.widgetContent,
    this.backgroundImage,
    this.defaultVal,
    this.nameController,
    this.passwordController,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LoginPage(
        title: title,
        widgetContent: this.widgetContent,
        backgroundImage: this.backgroundImage,
        defaultVal:this.defaultVal,
        nameController:this.nameController,
        passwordController:this.passwordController,
        onLogin: (option) {
          this.onLogin(option);
        });
  }
}

typedef WisHttpCallBack = void Function(DataResult httpErrorResult);

class WisHttp {
  //默认token 如果需要替换在此处重新置换token
  static Dio initTokenInterceptor({InterceptorsWrapper tokenInterceptor}){
    if(tokenInterceptor != null){
      HttpManage.setTokenInterceptor(tokenInterceptor);
    }
    return HttpManage.getInitDio();
  }

  static login(userName, password, url,
      {WisHttpCallBack success, WisHttpCallBack error}) {
    HttpManage.login(userName, password, "zh_CN", url).then((val) {
      DataResult data = val;
      if (data.success) {
        success(data);
      } else {
        error(data);
      }
    });
  }

  static post(url, params, {WisHttpCallBack success, WisHttpCallBack error}) {
    HttpManage.request(url, params, new Options(method: "post")).then((val) {
      DataResult data = val;
      if (data.success) {
        success(data);
      } else {
        error(data);
      }
    });
  }

  static get(url, params,
      {WisHttpCallBack success, WisHttpCallBack error}) async {
    return await HttpManage.request(url, params, new Options(method: "get"))
        .then((val) {
      DataResult data = val;
      if (data.success) {
        success(data);
      } else {
        error(data);
      }
    });
  }

  static download(url, name, BuildContext context,
      {WisHttpCallBack success, WisHttpCallBack error}) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('文件下载'),
            content: Text('是否下载：$name ?'),
            actions: <Widget>[
              FlatButton(
                child: Text("文件下载"),
                onPressed: () async {
                  HttpManage.download(url, name, context).then((responce) {
                    if (responce.statusCode == 200) {
                      if (success != null) {
                        success(new DataResult(
                            true, "download success", responce.statusMessage));
                      }
                    } else {
                      if (error != null) {
                        error(new DataResult(
                            false, "download error", responce.statusMessage));
                      }
                    }
                    Navigator.pop(context);
                  });
                },
              ),
              FlatButton(
                child: Text("取消"),
                onPressed: () async {
                  Navigator.pop(context);
                },
              )
            ],
          );
        });
  }

  static upload(url, {WisHttpCallBack success, WisHttpCallBack error}) {
    HttpManage.upload(url).then((responce) {
      if(responce != null){
        if (responce.statusCode == 200) {
          var uploadData = jsonDecode(responce.toString());
          if (success != null) {
            success(new DataResult(true, "upload success", uploadData['data']));
          }
        } else {
          if (error != null) {
            error(new DataResult(false, "upload error", responce.statusMessage));
          }
        }
      }
    });
  }
}
